package cn.zzm.core.config;

import org.apache.activemq.ActiveMQConnectionFactory;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.jms.config.DefaultJmsListenerContainerFactory;
import org.springframework.jms.core.JmsTemplate;

/**
 * TODO(基本的mq配置).
 *
 * <p>TODO(基本的mq配置).
 *
 * <p>版权所有:Copyright (c) 2019<br/>
 *
 * @author 周昭名
 * @version V1.0
 * @since 2019/7/1 19:01
 */
@Configuration
public class ActiveMQLocalConfig {
    @Value("${spring.activemq.local.broker-url}")
    private String brokerUrl;
    @Value("${spring.activemq.local.user}")
    private String user;
    @Value("${spring.activemq.local.password}")
    private String password;

    @Bean(name="localActiveMQConnectionFactory")
    public ActiveMQConnectionFactory localActiveMQConnectionFactory(){
        ActiveMQConnectionFactory amf = new ActiveMQConnectionFactory(user,password,brokerUrl);
        return amf;
    }
    @Bean(name="jmsLocalQueueListenerContainerFactory")
    public DefaultJmsListenerContainerFactory jmsLocalQueueListenerContainerFactory() {
        DefaultJmsListenerContainerFactory factory = new DefaultJmsListenerContainerFactory();
        factory.setConnectionFactory(localActiveMQConnectionFactory());
        return factory;
    }

    @Bean(name="jmsLocalTopicListenerContainerFactory")
    public DefaultJmsListenerContainerFactory jmsLocalTopicListenerContainerFactory() {
        DefaultJmsListenerContainerFactory factory = new DefaultJmsListenerContainerFactory();
        factory.setConnectionFactory(localActiveMQConnectionFactory());
        factory.setPubSubDomain(true);
        return factory;
    }

    @Bean(name="jmsLocalTemplate")
    public JmsTemplate jmsLocalTemplate(){
        JmsTemplate jmsTemplate = new JmsTemplate();
        jmsTemplate.setConnectionFactory(localActiveMQConnectionFactory());
        return jmsTemplate;
    }
}
